let axios = require('axios')
const md5 = require('blueimp-md5')

class BdMapSnUrl {//百度地图的sn值使用
  constructor() {
    this.yourak = 'UpAanbrF4YI60NUwsjoYqkF6dLt0NXEE'
    this.yoursk = 'BAGT6EzyCfwEg0dA6bRDnCUZD3q7d5vu'
  }

  getPoiDetailSnUrl(uid) {
    this.poiDetailUrl = `https://api.map.baidu.com/place/v2/detail?uid=${uid}&output=json&scope=2&ak=${this.yourak}&sn=`
    let params = {uid, output: 'json', scope: '2', ak: this.yourak}
    let paramsStr = Object.keys(params).map(key => key + "=" + encodeURIComponent(params[key])).join("&")
    let wholeStr = `/place/v2/detail?${paramsStr}${this.yoursk}`
    let tempStr = encodeURIComponent(wholeStr)
    let snValue = md5(tempStr)
    return this.poiDetailUrl + snValue
  }

}

// let bdMap = new BdMapSnUrl()
// console.log(bdMap.getPoiDetailSnUrl('82bd049d61e7631baaf72965'))
// console.log(bdMap.getPoiDetailSnUrl('ae9081579d93b6fc6e20b953'))

class Maps {
  constructor() {
    this.mPhones = ['13614745452', '15247439820', '15847403483']
    //高德地图
    this.zshlm_gd_url = 'https://restapi.amap.com/v3/place/detail?id=B0FFIPJE9Z&key=ca3c313ce11d86863c7a3e4f7d047bf0'
    this.hynjc_gd_url = 'https://restapi.amap.com/v3/place/detail?id=B0FFF4VIBD&key=7be44a796cd9dd0783c3be5314f3fe35'
    //百度地图
    this.bdMap = new BdMapSnUrl()
    this.zshlm_bd_url = this.bdMap.getPoiDetailSnUrl('82bd049d61e7631baaf72965')
    this.hynjc_bd_url = this.bdMap.getPoiDetailSnUrl('ae9081579d93b6fc6e20b953')
    //腾讯地图
    this.zshlm_tx_url = 'https://apis.map.qq.com/ws/place/v1/detail?id=2352765745981948316&key=MHLBZ-EWRCK-BKGJL-A5CQW-6SVT3-Q5B7E'
    this.hynjc_tx_url = 'https://apis.map.qq.com/ws/place/v1/detail?id=16610621678819913367&key=MHLBZ-EWRCK-BKGJL-A5CQW-6SVT3-Q5B7E'
  }

  async getMapInfo(url) {
    const options = {method: 'GET', url};
    let {data} = await axios(options)
    return data
  }

  async checkGdMapInfo() {
    let mapRes1 = await this.getMapInfo(this.zshlm_gd_url)
    let mapRes2 = await this.getMapInfo(this.hynjc_gd_url)
    console.log("mapRes1", mapRes1)
    if (mapRes1.pois) mapRes1.pois = mapRes1.pois.concat(mapRes2.pois)
    return mapRes1
  }


  async checkBdMapInfo() {
    let mapRes1 = await this.getMapInfo(this.hynjc_bd_url)
    let mapRes2 = await this.getMapInfo(this.zshlm_bd_url)
    mapRes1.result = [mapRes1.result]
    mapRes2.result = [mapRes2.result]
    mapRes1.result = mapRes1.result.concat(mapRes2.result)
    return mapRes1
  }

  async checkTxMapInfo() {
    let mapRes1 = await this.getMapInfo(this.zshlm_tx_url)
    let mapRes2 = await this.getMapInfo(this.hynjc_tx_url)
    if (mapRes1.data) mapRes1.data = mapRes1.data.concat(mapRes2.data)
    return mapRes1
  }

  async checkMapInfo(type) {
    if (type == 'gd') return await this.checkGdMapInfo()
    if (type == 'tx') return await this.checkTxMapInfo()
    if (type == 'bd') return await this.checkBdMapInfo()
    if (!type) return "参数为空！"
  }

  checkPhoneErr(phone) {
    let splitSpan = ' '
    if (phone && phone.indexOf(';') != -1) splitSpan = ';'
    if (phone && phone.indexOf(',') != -1) splitSpan = ','
    let phoneList = phone && phone.split(splitSpan)
    let notInItem = phoneList && phoneList.length && phoneList.find(item => this.mPhones.indexOf(item) == -1)
    return notInItem ? true : false
  }

  async warningToWechat(title, content) {
    title = encodeURIComponent(title)
    content = encodeURIComponent(content)
    let url = `https://sctapi.ftqq.com/SCT202400T9XW2RgqlyvzkyMVcJLHniZ7Y.send?title=${title}&desp=${content}`
    return await this.getMapInfo(url)
  }

  async checkMapPhone() {
    let {pois: gdList} = await this.checkGdMapInfo()
    if (gdList) gdList = gdList.map(item => item.tel || item.telephone)
    let gdHaveErr = gdList && gdList.find(item => this.checkPhoneErr(item))
    let {data: txList} = await this.checkTxMapInfo()
    if (txList) txList = txList.map(item => item.tel || item.telephone)
    let txHaveErr = txList && txList.find(item => this.checkPhoneErr(item))
    let {result: bdList} = await this.checkBdMapInfo()
    if (bdList) bdList = bdList.map(item => item.tel || item.telephone)
    let bdHaveErr = bdList && bdList.find(item => this.checkPhoneErr(item))
    if (gdHaveErr || txHaveErr || bdHaveErr) await this.warningToWechat("地图关联手机号被修改提醒！", "https://static-mp-67dcfd0c-c143-49dd-81a6-3d6aabd6ef7b.next.bspapp.com/first/#/pages/mine/map/map")
    return {
      gdList: gdList ? gdList : [], gdHaveErr: gdHaveErr ? true : false,
      txList: txList ? txList : [], txHaveErr: txHaveErr ? true : false,
      bdList: bdList ? bdList : [], bdHaveErr: bdHaveErr ? true : false
    }
  }
}

module.exports = new Maps()
